﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class ShelvesRepository : BaseRepository<TShelf>, IShelvesRepository
    {
        public ShelvesRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }
        /// <summary>
        /// 添加货架
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddShel(ShelvesDto obj, out string message)
        {
            message = "";
            try
            {
                var sobj = Context.TShelves.AsNoTracking().FirstOrDefault(x => x.Code == obj.Code);
                if(sobj!=null)
                {
                    message = "编码已存在！";
                    return false;
                }
                TShelf shelf = new TShelf();
                shelf = Mapper.Map<TShelf>(obj);
                shelf.CreateUserId = obj.NowUserId;
                shelf.CreateUserName = obj.NowUserRealName;
                shelf.CreateDate = DateTime.Now;
                shelf.ModifyUserId = obj.NowUserId;
                shelf.ModifyUserName = obj.NowUserRealName;
                shelf.ModifyDate = DateTime.Now;
                //库房
                var wobj = Context.TWarehouses.Find(obj.WhId);
                if(wobj!=null && wobj.Status==1) 
                {
                    shelf.WhName = wobj.Name;
                }
                else
                {
                    message = "库房不存在或已被禁用！";
                    return false;
                }
                //库区
                var aobj=Context.TAreas.Find(obj.AreaId);
                if(aobj!=null && aobj.Status==1) 
                {
                    shelf.AreaName = aobj.Name;
                    shelf.AreaCode = aobj.Code;
                }            
                else
                {
                    message = "库区不能再或已被禁用！";
                    return false;
                }
                Context.TShelves.Add(shelf);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除货架
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelShel(int uid)
        {
            try
            {
                var del = Context.TShelves.Find(uid);
                if (del != null)
                {
                    Context.TShelves.Remove(del);
                    return Context.SaveChanges() > 0;
                }
                return false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id获取货架信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TShelf GetShelById(int id)
        {
            try
            {
                return Context.TShelves.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示货架信息
        /// </summary>
        /// <param name="hcode"></param>
        /// <param name="hname"></param>
        /// <param name="kid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<ShelvesDto> GetShelList(string hname, int kid, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var alist = Context.TShelves.AsQueryable();//货架
                var blist = Context.TWarehouses.AsQueryable();//库房
                var clist = Context.TAreas.AsQueryable();//库区
                var dlist = Context.TRouteways.AsQueryable();//巷道
                if (!string.IsNullOrEmpty(hname))
                {
                    alist = alist.Where(c => c.Name.Contains(hname));
                }
                if (kid > 0)
                {
                    alist = alist.Where(x => x.WhId == kid);
                }
                var query = from A in alist
                            join B in blist
                            on A.WhId equals B.Id
                            join C in clist
                            on A.AreaId equals C.Id
                            join D in dlist
                            on A.RoutewayId equals D.Id
                            select new ShelvesDto
                            {
                                Id = A.Id,
                                Code = A.Code,
                                Name = A.Name,
                                Number = A.Number,
                                AreaCode = A.AreaCode,
                                AreaName = A.AreaName,
                                Status = A.Status,
                                DisplayOrder = A.DisplayOrder,
                                Remark = A.Remark,
                                CreateDate = A.CreateDate,
                                CreateUserId = A.CreateUserId,
                                CreateUserName = A.CreateUserName,
                                ModifyDate = A.ModifyDate,
                                ModifyUserId = A.ModifyUserId,
                                ModifyUserName = A.ModifyUserName,
                                WhName = A.WhName,
                                CellCount = A.CellCount,
                                DisabledCellCount = A.DisabledCellCount,
                                Depth = A.Depth,
                                ShelfType = A.ShelfType,
                                //FId=B.Id,
                                FName = B.Name,
                                //KId=C.Id,
                                KName = C.Name,
                                //XId=D.Id,
                                XName = D.Name,
                            };
                totalCount = query.Count();
                query = query.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return query.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取巷道信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TRouteway> GetTRoutewayList()
        {
            try
            {
                return Context.TRouteways.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取库房(仓库)信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TWarehouse> GetTwarehouseList()
        {
            try
            {
                return Context.TWarehouses.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改货架信息
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdateShel(ShelvesDto u,out string message)
        {
            message = "";
            try
            {
                var list = Context.TShelves.AsNoTracking().FirstOrDefault(x => x.Id != u.Id && x.Code == u.Code);

                if (list != null)
                {
                    message = "编码已存在！";
                    return false;
                }
                TShelf shelf = new TShelf();
                shelf = Mapper.Map<TShelf>(u);
                u.ModifyDate = DateTime.Now;
                u.ModifyUserId = u.NowUserId;
                u.ModifyUserName = u.NowUserRealName;
                //库房
                var wobj = Context.TWarehouses.Find(u.WhId);
                if (wobj != null && wobj.Status==1)
                {
                    shelf.WhName = wobj.Name;
                }
                else
                {
                    message = "库房不存在或已被禁用！";
                    return false;
                }
                //库区
                var aobj = Context.TAreas.Find(u.AreaId);
                if (aobj != null && aobj.Status==1)
                {
                    shelf.AreaName = aobj.Name;
                    shelf.AreaCode = aobj.Code;
                }
                else
                {
                    message = "库区不存在或已被禁用！";
                    return false;
                }
                Context.TShelves.Update(shelf);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 所属库区级联展示
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Menuitem> GetWarehouseMenuitem()
        {
            var wlist = Context.TWarehouses.AsNoTracking().ToList();

            return wlist.Select(item => new Menuitem
            {
                label = item.Name,
                value = item.Id,
                icon=item.Code,
                children = GetAreaMenuitem(item.Id)
            }).ToList();
        }

        public List<Menuitem> GetAreaMenuitem(int wid)
        {
            var list = Context.TAreas.AsNoTracking().Where(x => x.WhId == wid);
            return list.Select(x => new Menuitem
            {
                label = x.Name,
                value = x.Id,
                icon = x.Code,
                children=null
            }).ToList();
        }

        /// <summary>
        /// 根据库区获取货架
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TShelf> GetAreaShelf(int aid)
        {
            var shelf = Context.TShelves.Where(x => x.AreaId == aid);
            return shelf.ToList();

        }
    }
}
